﻿package lz.controls
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.core.UIComponent;
	import lz.controls.events.ScrollEvent;
	/**
	 * 滚动条组件(固定)
	 * @author lake
	 *  
	 * var lScrollBar :LScrollBar = new LScrollBar();
	 lScrollBar.x = 300;
	 lScrollBar.y = 50;
	 lScrollBar.height = 200;//高度
	 lScrollBar.maxScrollPosition = 100;//最大滚动位置，比如testMC 高度为300，maxScrollPosition = 300 -lScrollBar.height； 
	 this.addChild(lScrollBar);
	 
	 testMC.scrollRect = new Rectangle(0,0,300,200);
	 
	 lScrollBar.addEventListener(ScrollEvent.SCROLL,onScroll);
	 function onScroll(e :ScrollEvent):void
	 {
	 var rect :Rectangle = testMC.scrollRect;
	 rect.y = e.position;
	 testMC.scrollRect = rect;
	 }
	 */	
	public class LScrollBar extends UIComponent
	{
		/**
		 * 滑块
		 */		
		private var __dragBar	:SimpleButton;
		/**
		 * track 背景区域
		 */		
		private var __area		:Sprite;
		/**
		 * 下按钮
		 */		
		private var __downBtn	:SimpleButton;
		/**
		 * 上按钮
		 */		
		private var __upBtn	:SimpleButton;
		
		public static var WIDTH			:Number ;
		public static var MINHEIGHT		:Number;
		public static const HORIZONTAL 	:String = "horizontal";
		public static const VERTICAL 		:String = "vertical";
		public static const REPEATTIMER	:Number=100;
		public static const SCROLLGRAP 	:Number = 0;
		
		private var _direction				:String = VERTICAL;
		private var _lineScrollSize		:Number = 10;
		private var _maxScrollPosition		:Number = 100;
		private var _minScrollPosition		:Number = 0;
		private var _scrollPosition		:Number = 0;
		
		private var _dragBarminHeight		:Number = 20;
		private var _startDragY			:Number = 0;
		/**
		 *临时 
		 */		
		private var dragTempBarHeight		:Number = 0;
		
		public function LScrollBar()
		{
			super();
		}
		/**
		 * configUI
		 * 
		 */		
		protected override function configUI():void
		{
			skin = "SkinScrollBar";
			
			
			WIDTH    	= __area.width;
			MINHEIGHT 	= __downBtn.height + __upBtn.height + 10
		}
		/**
		 * addScrollBarListener
		 * 
		 */		
		private function addScrollBarListener():void
		{
			__upBtn.addEventListener(MouseEvent.MOUSE_DOWN,pressUp);
			__downBtn.addEventListener(MouseEvent.MOUSE_DOWN,pressDown);
			
			__dragBar.addEventListener(MouseEvent.MOUSE_DOWN,scrollDargHandle);
			
			__area.addEventListener(MouseEvent.MOUSE_DOWN,areaDownHandle);
			
			this.addEventListener(Event.REMOVED_FROM_STAGE,removedHandle);
		}
		/**
		 * removedHandle
		 * @param e
		 * 
		 */		
		private function removedHandle(e:Event):void
		{
			if(stage.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragMove);
			}
			if(stage.hasEventListener(MouseEvent.MOUSE_UP))
			{
				stage.removeEventListener(MouseEvent.MOUSE_UP,stopMove);			
			}
		}
		/**
		 * areaDownHandle
		 * @param e
		 * 
		 */		
		private function areaDownHandle(e:MouseEvent):void
		{
			var pos:Number = Math.max(_minScrollPosition,
				Math.min((mouseY - (__upBtn.height + SCROLLGRAP))/__area.height * _maxScrollPosition,_maxScrollPosition));
			addEventListener(Event.ENTER_FRAME,enterFrameHandle);
			stage.addEventListener(MouseEvent.MOUSE_UP,areaUpHandle)
			function enterFrameHandle(e:Event):void
			{
				scrollPosition -= (scrollPosition - Math.round(pos))*0.1;
			}
			function areaUpHandle(e:MouseEvent):void
			{
				removeEventListener(Event.ENTER_FRAME,enterFrameHandle);
			}
		}
		/**
		 * skin
		 * @param value
		 * 
		 */		
		public function set skin(value:String):void
		{
			DisplayObjectUtil.removeAllChildren(this,true);
			_symbolSkin = Reflection.createInstance(value);
			if(_symbolSkin == null)
			{
				return
			}
			this.addChild(_symbolSkin);
			__dragBar	= _symbolSkin.__dragBar;
			__area 		= _symbolSkin.__area;
			__downBtn 	= _symbolSkin.__downBtn;
			__upBtn 	= _symbolSkin.__upBtn;
			
			_width 		= _symbolSkin.width;
			_height 	= _symbolSkin.height;
			
			layout();
			addScrollBarListener();
		}
		/**
		 * layout
		 * 
		 */		
		protected function layout():void
		{
			__area.y = __upBtn.y + __upBtn.height + SCROLLGRAP;
			__area.height = this._height - __upBtn.height - __downBtn.height - 2 * SCROLLGRAP;
			
			__downBtn.y =  __area.height + __area.y + __downBtn.height + SCROLLGRAP;
			_scrollPosition = Math.max(_minScrollPosition,Math.min(_scrollPosition,_maxScrollPosition));
			dragTempBarHeight = Math.max(_dragBarminHeight,(__area.height - (_maxScrollPosition - _minScrollPosition)));
			__dragBar.y = (__upBtn.height + SCROLLGRAP) 
				+ ((_scrollPosition -_minScrollPosition)/(_maxScrollPosition - _minScrollPosition)) * (__area.height - __dragBar.height);
		}
		/**
		 * scrollDargHandle
		 * @param e
		 * 
		 */		
		private function scrollDargHandle(e:MouseEvent):void
		{
			_startDragY = mouseY;
			stage.addEventListener(MouseEvent.MOUSE_MOVE,dragMove);			
			stage.addEventListener(MouseEvent.MOUSE_UP,stopMove);			
		}
		/**
		 * dragMove
		 * @param e
		 * 
		 */		
		private function dragMove(e:MouseEvent):void
		{
			var pos:Number = Math.max(_minScrollPosition,
				Math.min((mouseY - (__upBtn.height + SCROLLGRAP))/__area.height * _maxScrollPosition,_maxScrollPosition));
			scrollPosition = Math.round(pos);
		}
		/**
		 * stopMove
		 * @param e
		 * 
		 */		
		private function stopMove(e:MouseEvent):void
		{
			if(stage.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragMove);
			}
			stage.removeEventListener(MouseEvent.MOUSE_UP,stopMove);			
		}
		/**
		 * pressUp
		 * @param e
		 * 
		 */		
		private function pressUp(e:MouseEvent):void
		{
			if(scrollPosition-lineScrollSize>_minScrollPosition)
			{
				scrollPosition -= lineScrollSize;
			}
			else
			{
				scrollPosition = _minScrollPosition;
			}
		}
		/**
		 * pressDown
		 * @param e
		 * 
		 */		
		private function pressDown(e:MouseEvent):void
		{
			if(scrollPosition+lineScrollSize < _maxScrollPosition)
			{
				scrollPosition+=lineScrollSize;
			}
			else
			{
				scrollPosition=_maxScrollPosition
			}
		}
		/**
		 * direction
		 * @param value
		 * 
		 */		
		public function set direction(value:String):void
		{
			_direction = value;
			if(_direction == VERTICAL)
			{
				this.rotation = 0;
				this.scaleX = 1;
			}
			else if(_direction == HORIZONTAL)
			{
				this.rotation = -90;
				this.scaleX = -1;
			}
		}
		public function get direction():String
		{
			return _direction;
		}
		override public function set height(value:Number):void
		{
			_height = Math.max(MINHEIGHT,value);
			layout();
		}
		override	public function get height():Number
		{
			return _height;
		}
		public function set lineScrollSize(value:Number):void
		{
			_lineScrollSize = value
			
		}
		public function get lineScrollSize():Number
		{
			return _lineScrollSize;
		}
		
		public function set maxScrollPosition(value:Number):void
		{
			_maxScrollPosition = value
			if(_maxScrollPosition <= 0)
			{
				this.visible = false;
			}
			else
			{
				this.visible = true;
			}
			layout();
		}
		public function get maxScrollPosition():Number
		{
			return _maxScrollPosition;
		}
		
		public function set minScrollPosition(value:Number):void
		{
			_minScrollPosition = value;
			layout();
		}
		public function get minScrollPosition():Number
		{
			return _minScrollPosition;
		}
		public function set scrollPosition(value:Number):void
		{
			var oldPosition:Number = _scrollPosition;
			_scrollPosition = Math.max(_minScrollPosition,Math.min(value,_maxScrollPosition));
			dispatchEvent(new ScrollEvent(_direction, _scrollPosition-oldPosition, _scrollPosition));
			layout();
		}
		public function get scrollPosition():Number
		{
			return _scrollPosition;
		}
		/**
		 * 
		 * 
		 */
		override public function destroy():void
		{
			super.destroy();
			__upBtn.removeEventListener(MouseEvent.MOUSE_DOWN,pressUp);
			__downBtn.removeEventListener(MouseEvent.MOUSE_DOWN,pressDown);
			
			__dragBar.removeEventListener(MouseEvent.MOUSE_DOWN,scrollDargHandle);
			
			__area.removeEventListener(MouseEvent.MOUSE_DOWN,areaDownHandle);
			if(stage != null)
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragMove);
				stage.removeEventListener(MouseEvent.MOUSE_UP,stopMove);	
			}
			
			this.removeEventListener(Event.REMOVED_FROM_STAGE,removedHandle);
			DisplayObjectUtil.removeAllChildren(this,true);
			_symbolSkin = null;
			__dragBar = null;
			__downBtn = null;
			__upBtn = null;
			__area = null;
		}
	}
}